package leetcode.editor.cn.q1_300.q50;

import java.util.Stack;

/**
 * @author qidi
 * @date 2021-12-15 13:17:31
 */
class Q0071_SimplifyPath {

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public String simplifyPath(String path) {
            Stack<String> stack = new Stack<>();
            String[] strs = path.split("/");
            for (String str : strs) {
                if (str.length() == 0 || str.equals(".")) continue;
                if (str.equals("..")) {
                    if (stack.size() > 0) stack.pop();
                } else {
                    stack.add(str);
                }
            }

            if (stack.isEmpty()) {
                return "/";
            }

            StringBuilder sb = new StringBuilder();
            while (!stack.isEmpty()) {
                sb.insert(0, "/" + stack.pop());
            }

            return sb.toString();
        }
    }
    //leetcode submit region end(Prohibit modification and deletion)


    public static void main(String[] args) {
        Solution solution = new Q0071_SimplifyPath().new Solution();
        System.out.println(solution.simplifyPath("/../"));
        System.out.println(solution.simplifyPath("/home//foo/"));
        System.out.println(solution.simplifyPath("/a/./b/../../c/"));
    }
}